package com.demo.action;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.json.annotations.JSON;
import org.hibernate.HibernateException;

import com.demo.entity.Userinfo;
import com.demo.otherbeans.SystemInfoBean;
import com.demo.services.UserServices;
import com.demo.tools.StaticFreemarker;
import com.demo.tools.SystemInfo;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.interceptor.ValidationWorkflowAware;

import freemarker.template.TemplateException;

/**
 * 注意：
 * 1、实现ModelDriven接口，简写前台entity.字段写法
 * 2、实现ValidationWorkflowAware接口，
 *    改变当action中有多个方法需要校验时
 *    检验失败后只能返回input视图的局限性
 * 3、实现   Preparable接口
 * 	  并在实现方法中调用clearErrorsAndMessages()解决验证时错误信息累加显示
 * 4、在前台jquery getJson请求传输数据时
 *    在action中讲不需要传入前台的属性get方法
 *    加上@JSON(serialize=false)，其值就不会传入前台，避免出错。
 * 
 * @author 曾庆博
 *
 */ 


public class LoginAction extends ActionSupport implements ModelDriven<Userinfo>,ValidationWorkflowAware,Preparable {
	
	private static Logger log = Logger.getLogger(LoginAction.class);	
	
	public Userinfo userinfo; //entity
	public UserServices userServices;//接口
	public SystemInfoBean systemInfoBean;//系统信息bean
	private String inputResultName;//重写ValidationWorkflowAware中参数方法 
	private String redirectHtmlPath;//重定向htmlpath
	 
	
	public String freemarkerGetSystemInfo(){
		return "getSystemInfoSuccess";
	}
	
	/**
	 *访问action，freemarker生成html
	 * @return
	 * @throws IOException
	 * @throws TemplateException
	 */
	public String freemarkerGetSystemInfo2() throws IOException, TemplateException{
		
		List tempTestList = new ArrayList();
		
		
		tempTestList.add(new SystemInfo().getSystemInfo()); 
		StaticFreemarker freemarker = new StaticFreemarker(); 
		String htmlFileName = "systeminfo.html";
		String htmlFilePath = "thread/";
		String ftlPath = "template";//模板相对项目root路径
		String ftlName = "index.flt"; 
		Map  map = new HashMap();
		map.put("tempTestList",tempTestList);
		freemarker.init( ftlPath,ftlName,htmlFilePath ,htmlFileName, map, ftlPath);
		this.setRedirectHtmlPath(htmlFilePath+htmlFileName);
		
		
		setSystemInfoBean(new SystemInfo().getSystemInfo());
		return "getSystemInfoSuccess";
	}
	
	
	public String saveUser() throws IOException, SQLException{ 
		log.debug("excuce LoginAction.saveUser");  
		userServices.saveUser(userinfo);  
		return "success"; 
	} 
	
	public String userLogin() throws HibernateException, SQLException{
		log.debug("excuce LoginAction.userLogin");  
		String name = userinfo.getName();
		String sex = userinfo.getSex();    
		String hql = " from Userinfo  where name='"+name+"' and sex='"+sex+"'";
		List<Userinfo> list = userServices.findUser(hql);
		System.out.println("======"+userServices.getDatabaseType()); 
		if(list.size()>0){
			return "success";
		}else{
			return "error";
		} 
	}
	
	public String userSave(){
		log.debug("execute LoginAction.userSave");  
		userServices.saveUser(userinfo); 
		return "saveSuccess";
	}
	
	
	/**
	 * 获取系统运行信息
	 * @return
	 */
	@JSON(serialize=false) 
	public String getSystemInfo(){  
		systemInfoBean = new SystemInfoBean();
		systemInfoBean =  new SystemInfo().getSystemInfo(); 
		return  "systeminfo"; 
	}
	
	 
 
	@JSON(serialize=false) 
	public Userinfo getUserinfo() {
		return userinfo;
	}
	
	public void setUserinfo(Userinfo userinfo) {
		this.userinfo = userinfo;
	}  
	
	
	@JSON(serialize=false)
	public Userinfo getModel() {
		if(null == userinfo){
			userinfo = new Userinfo();
		}
		return userinfo;
	}
	 
	@JSON(serialize=false)
	public UserServices getUserServices() {
		return userServices;
	}

	public void setUserServices(UserServices userServices) {
		this.userServices = userServices;
	}
	
	@JSON(serialize=false)
	public String getInputResultName() {
		return inputResultName;
	}

	public void setInputResultName(String inputResultName) {
		this.inputResultName = inputResultName;
	}

	public void prepare() throws Exception {
		// TODO Auto-generated method stub
		clearErrorsAndMessages();
	}

	public SystemInfoBean getSystemInfoBean() {
		return systemInfoBean;
	}

	public void setSystemInfoBean(SystemInfoBean systemInfoBean) {
		this.systemInfoBean = systemInfoBean;
	}

	@JSON(serialize=false)
	public String getRedirectHtmlPath() {
		return redirectHtmlPath;
	}

	public void setRedirectHtmlPath(String redirectHtmlPath) {
		this.redirectHtmlPath = redirectHtmlPath;
	}
 
    

}
